package com.aididi.sort;

import java.util.*;
import java.util.stream.Collector;

/**
 * @Auther: xuzexiang
 * @Date: 2018-11-1 0001 09:57
 * @Description:    排序自带的排序方法
 */
public class CollectionSortDemo {
    public static void main(String[] args) {
        int [] num = {1,2,8,5,-2,4,-9,10};
        Arrays.sort(num);
        Arrays.stream(num).forEach(System.out::println);

        User u1 = new User(4,"张三");
        User u2 = new User(5,"李四");
        User u3 = new User(3,"王五");
        List list = new ArrayList();
        list.add(u1);
        list.add(u2);
        list.add(u3);
        Collections.sort(list, new Comparator<User>() {

            @Override
            public int compare(User o1, User o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        System.out.println(list);

    }




}

class User{
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}
